package com.cip.master.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ArrayList;

import com.cip.admin.model.User;
import com.cip.common.util.SecurityUtil;
import com.cip.webapp.action.BaseAction;
import java.text.SimpleDateFormat;

import com.cip.master.model.Academic;
import com.cip.master.model.Examination;
import com.cip.master.model.HomeWork;
import com.cip.master.model.Result;
import com.cip.master.model.Section;
import com.cip.master.model.Subject;
import com.cip.master.service.AcademicService;
import com.cip.master.service.ClassYearService;
import com.cip.master.service.SectionService;
import com.cip.master.model.ClassYear;
import com.cip.master.service.SubjectService;
import com.cip.master.service.HomeWorkService;

public class HomeWorkAction extends BaseAction {
	private HomeWork homeWork;
	private AcademicService  academicService;
	private SubjectService  subjectService;
	private ClassYearService classYearService;
	private SectionService sectionService;
	private HomeWorkService  homeWorkService;
	private List<Section> sectionList;
	private List<Subject> subjectList;
	private List<HomeWork> homeList;
	private Map<Date,List<HomeWork>> homeWorkList=new HashMap<Date, List<HomeWork>>();
	private List<ClassYear> classYears;
	private String classYear;
	private String subject;
	private String section;	

	public String showHomeWorkList() throws Exception {
		
		Locale locale = getRequest().getLocale();
		String homeWorkString = homeWorkService.getHomeWorkGridList(locale);
				
		getRequest().setAttribute("homeWorkGridString", homeWorkString);
		
//		setHomeList(homeWorkService.getAllHomeWorkList());
		return SUCCESS;
	}
    public String showHomeworkForm()throws Exception{
    	setHomeWork(new HomeWork());
    	setClassYears(classYearService.getAllClassYear());
		setSubjectList(subjectService.getAllSubjectList());
		setSectionList(sectionService.getAllSection()); 
    	return SUCCESS;
    }
    
    public String saveOrUpdateHomeWork() throws Exception {
		User user = SecurityUtil.getThreadLocalUser().get();
		ClassYear classYr = classYearService.getClassYear(Integer.parseInt(classYear));
		Subject subject_obj=subjectService.getSubjectById(Integer.parseInt(subject));
		Section section_obj=sectionService.getSection(Integer.parseInt(section));
		homeWork.setTenantId(user.getTenantId());
	    homeWork.setClassYear(classYr);
		homeWork.setSection(section_obj);
		homeWork.setSubject(subject_obj);
		homeWorkService.saveOrUpdateHomeWork(homeWork);
	    saveMessage(getText("Assignment Successfully created  "));
		return SUCCESS;
	}
    
    public String editHomework() {
    	try{ 
    		homeWork=homeWorkService.getHomeWorkById(homeWork.getId());
			setClassYear(getHomeWork().getClassYear().getName());
			setSection(getHomeWork().getSection().getSectionName());
			setSubject(getHomeWork().getSubject().getSubjectName());
    		setHomeWork(homeWork);
    		setClassYears(classYearService.getAllClassYear());
    		setSubjectList(subjectService.getAllSubjectList());
    		setSectionList(sectionService.getAllSection());
    	}
    	catch (Exception e) {
			e.printStackTrace();
		}
    	return SUCCESS;
    }
    
	public String showUserHomework() {
		try { 
			Calendar date;
			//int year = Calendar.getInstance().get(Calendar.DATE);
			//String academicYear = (year-1) +"-"+year;
			Map<Date, List<HomeWork>> homeWorkMap = new HashMap<Date,  List<HomeWork>>();
			
			User user = SecurityUtil.getLoggedInUser();
			List<HomeWork> homeWorkList = homeWorkService.getAllHomeWorkListByClass(Integer.valueOf(user.getSection()),Integer.valueOf(user.getClassYear()));
			
			for (HomeWork homeWork : homeWorkList) {
				if(homeWorkMap.containsKey(homeWork.getDate())){
	                 List<HomeWork>	homeListByDate=homeWorkMap.get(homeWork.getDate());
	                 homeListByDate.add(homeWork);
	                 homeWorkMap.put(homeWork.getDate(),homeListByDate);
					}else{
						  List<HomeWork> homeListByDate = new ArrayList<HomeWork>();
						  homeListByDate.add(homeWork);
			              homeWorkMap.put(homeWork.getDate(),homeListByDate);
					}
			}
			setHomeWorkList(homeWorkMap);
			
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public HomeWork getHomeWork() {
		return homeWork;
	}
		public void setHomeWork(HomeWork homeWork) {
			this.homeWork = homeWork;
	}
	public SubjectService getSubjectService() {
		return subjectService;
	}
	public void setSubjectService(SubjectService subjectService) {
		this.subjectService = subjectService;
	}
	public ClassYearService getClassYearService() {
		return classYearService;
	}
	public void setClassYearService(ClassYearService classYearService) {
		this.classYearService = classYearService;
	}
	public SectionService getSectionService() {
		return sectionService;
	}
	public void setSectionService(SectionService sectionService) {
		this.sectionService = sectionService;
	}
	public HomeWorkService getHomeWorkService() {
		return homeWorkService;
	}
	public void setHomeWorkService(HomeWorkService homeWorkService) {
		this.homeWorkService = homeWorkService;
	}
	public List<Section> getSectionList() {
		return sectionList;
	}
	public void setSectionList(List<Section> sectionList) {
		this.sectionList = sectionList;
	}
	public List<Subject> getSubjectList() {
		return subjectList;
	}
	public void setSubjectList(List<Subject> subjectList) {
		this.subjectList = subjectList;
	}
	public List<ClassYear> getClassYears() {
		return classYears;
	}
	public void setClassYears(List<ClassYear> classYears) {
		this.classYears = classYears;
	}
	public String getClassYear() {
		return classYear;
	}
	public void setClassYear(String classYear) {
		this.classYear = classYear;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getSection() {
		return section;
	}
	public void setSection(String section) {
		this.section = section;
	}
	public List<HomeWork> getHomeList() {
		return homeList;
	}
	public void setHomeList(List<HomeWork> homeList) {
		this.homeList = homeList;
	}
	public AcademicService getAcademicService() {
		return academicService;
	}
	public void setAcademicService(AcademicService academicService) {
		this.academicService = academicService;
	}
	public Map<Date, List<HomeWork>> getHomeWorkList() {
		return homeWorkList;
	}
	public void setHomeWorkList(Map<Date, List<HomeWork>> homeWorkList) {
		this.homeWorkList = homeWorkList;
	}
	  
}
